package com.aaa.web.service.impl.permissionserviceimpl;
/**
* @program: ${PROJECT_NAME}
* @description: ${description}
* @author:
* @create:
**/
import com.aaa.entity.Employee;
import com.aaa.entity.Permission;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.EmpQuery;
import com.aaa.web.commons.Result;
import com.aaa.web.dao.ipermissiondao.IPermissionDao;
import com.aaa.web.service.ipermissionservice.IPermissionService;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private IPermissionDao iPermissionDao;


    @Override
    public List<Permission> getAllPermission(){
        Employee emp = EmpUtil.getEmp();
        Integer admin = emp.getAdmin();
        List<Permission> permissionList = null;
        if(admin==1){
            permissionList = iPermissionDao.getAllPermissionParent();
            for (int i = 0; i < permissionList.size(); i++) {
                Integer permissionid = permissionList.get(i).getPermissionid();
                List<Permission> permissionList1 = iPermissionDao.getAllPermissionChild(permissionid);
                for (int j = 0; j <permissionList1.size() ; j++) {
                    if(permissionList1.get(j).getPermissionid()==4){
                        permissionList1.remove(j);
                    }
                    permissionList.get(i).setPermissionList(permissionList1);
                }
            }
        }else{
            permissionList = iPermissionDao.getPermissionParentByEmp(emp.getEmployeeid());
            Map map = new HashMap();
            map.put("employeeid",emp.getEmployeeid());
            for (int i = 0; i < permissionList.size(); i++) {
                Integer permissionid = permissionList.get(i).getPermissionid();
                map.put("permissionid",permissionid);
                List<Permission> permissionList1 = iPermissionDao.getPermissionChildByEmp(map);
                for (int j = 0; j <permissionList1.size() ; j++) {
                    if(permissionList1.get(j).getPermissionid()==44){
                        permissionList1.remove(j);
                    }
                    permissionList.get(i).setPermissionList(permissionList1);
                }
            }
        }
        return permissionList;

    }

    @Override
    public List<Map> getPermission(EmpQuery query) {
        return iPermissionDao.getPermission();
    }

    @Override
    public Result updatePerState(Integer id) {
        Integer state = iPermissionDao.getStateById(id);
        Map map = new HashMap();
        map.put("id",id);
        map.put("state",state);
        Integer flag=iPermissionDao.updatePerState(map);
        Result result = new Result();
        if(flag>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }

    @Override
    public Result editPer(Permission permission) {
        Integer flag=iPermissionDao.editPer(permission);
        Result result = new Result();
        if(flag>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }


}
